package com.cares.common.dao.orm.hibernate;


import java.io.Serializable;
import java.util.List;

import org.hibernate.LockMode;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.sh.cares.core.pagination.OrderablePagination;
import cn.sh.cares.core.pagination.Pagination;
import cn.sh.cares.dao.DaoException;
import cn.sh.cares.dao.orm.hibernate.query.SafeDetachedCriteria;

import com.cares.common.dao.orm.OrmExecution;

/**
 * hibernate orm 接口
  * Copyright: Copyright (c) 2011  
  * Company: 上海民航华东凯亚系统集成有限公司  
  * @author: 
  * @version: 
  * @date：Nov 29, 2011 2:44:05 PM
 */
public interface HibernateOrmExecution extends OrmExecution {
    
    <T> T get(Class<T> entityClass, Serializable id, LockMode lockMode) throws DaoException;
    
    <T> T get(Class<T> entityClass, Serializable id, LockMode lockMode, String[] initFields) throws DaoException;
    
    /**
     * 基于hibernate的DetachedCriteria实现分页查询，DetachedCriteria详细参见hibernate文档
     * OrderablePagination里包含分页信息，也包括排序信息，OrderablePagination的排序并不会替换DetachedCriteria的排序
     * @param T 返回List中对象的类型
     * @param detachedCriteria
     * @param op
     * @return
     */
    <T> List<T> queryForListByCriteria(Class<T> returnType, SafeDetachedCriteria safeDetachedCriteria, OrderablePagination op) throws DaoException;
    
    <T> List<T> queryForListByCriteria(Class<T> returnType, SafeDetachedCriteria safeDetachedCriteria) throws DaoException;

    /**
     * 对于给定的DetachedCriteria返回单个对象，如果返回结果为null，则返回defaultValue。
     * 这个对象可以是任何类型，如Integer、Long或者BaseEntity。
     * 如果clz为BaseEntity，那么detachedCriteria的entity class必须clz一致
     * 其它一般的类型需要设置DetachedCriteria.setProjection方法。
     * 
     * @param <T>
     * @param returnType 返回的Class类型，不是DetachedCriteria里的entity class
     * @param detachedCriteria
     * @param defaultValue
     * @return
     */
    <T> T queryForUniqueObjectByCriteria(Class<T> returnType, SafeDetachedCriteria safeDetachedCriteria, T defaultValue) throws DaoException;
    
    <T> T queryForUniqueObjectByCriteria(Class<T> returnType, SafeDetachedCriteria safeDetachedCriteria) throws DaoException;
    
    <T> List<T> queryForListByHql(Class<T> returnType, String hql, Object[] params, Pagination pagination) throws DaoException;

    <T> List<T> queryForListByHql(Class<T> returnType, String hql, Pagination pagination) throws DaoException;

    <T> List<T> queryForListByHql(Class<T> returnType, String hql, Object[] params) throws DaoException;

    <T> List<T> queryForListByHql(Class<T> returnType, String hql) throws DaoException;

    /**
     * 执行给定的hql，返回单个对象T，如果返回结果为null，则返回defaultValue。
     * 
     * @param <T>
     * @param clz
     * @param hql
     * @param params
     * @param defaultValue
     * @return
     */
    <T> T queryForUniqueObjectByHql(Class<T> returnType, String hql, Object[] params, T defaultValue) throws DaoException;

    <T> T queryForUniqueObjectByHql(Class<T> returnType, String hql, Object[] params) throws DaoException;

    <T> T queryForUniqueObjectByHql(Class<T> returnType, String hql, T defaultValue) throws DaoException;

    <T> T queryForUniqueObjectByHql(Class<T> returnType, String hql) throws DaoException;

    /**
     * 执行更新或者保存hql，返回hql执行所影响的记录个数。
     * @param hql
     * @param params
     * @return
     */
    int executeHql(String hql, Object... params) throws DaoException;

    int executeHql(String hql) throws DaoException;
    
    void lock(Object entity, LockMode lockMode) throws DaoException;
    
    void lock(String entityName, Object entity, LockMode lockMode) throws DaoException;
    
    /**
     * 对外暴露hibernate实体操作类，供子类获取，提供log数据存储的数据库入口 
     */
    HibernateTemplate getHibernateTemplate();
     
}